﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QUT.Bio.Util {
	/// <summary>
	/// A portable, if somewhat odd-looking, facility for doing user dialogs.
	/// </summary>
	
	public class PopupDialog : PopupWindow {
		private DialogAction action = DialogAction.Cancel;

		public PopupDialog() {
			Grid buttonGrid = new Grid();

			buttonGrid.ColumnDefinitions.Add( new ColumnDefinition() {
				Width = new GridLength( 1, GridUnitType.Star )
			} );
			buttonGrid.ColumnDefinitions.Add( new ColumnDefinition() {
				Width = new GridLength( 1, GridUnitType.Auto )
			} );
			buttonGrid.ColumnDefinitions.Add( new ColumnDefinition() {
				Width = new GridLength( 5, GridUnitType.Pixel )
			} );
			buttonGrid.ColumnDefinitions.Add( new ColumnDefinition() {
				Width = new GridLength( 1, GridUnitType.Auto )
			} );
			buttonGrid.ColumnDefinitions.Add( new ColumnDefinition() {
				Width = new GridLength( 1, GridUnitType.Star )
			} );

			Button okButton = new Button();
			okButton.Content = "Ok";
			okButton.Width = 50;
			okButton.SetValue( Grid.ColumnProperty, 1 );
			okButton.Margin = new Thickness( 0, 3, 0, 3 );
			okButton.Click += OkButtonClicked;
			buttonGrid.Children.Add( okButton );

			Button cancelButton = new Button();
			cancelButton.Content = "Cancel";
			cancelButton.Width = 50;
			cancelButton.SetValue( Grid.ColumnProperty, 3 );
			cancelButton.Margin = new Thickness( 0, 3, 0, 3 );
			cancelButton.Click += CancelButtonClicked;
			buttonGrid.Children.Add( cancelButton );

			content.South = buttonGrid;

			base.WindowChanged += OnWindowChange;
		}

		private void OnWindowChange( PopupWindow w, WindowChangeEventType windowChange ) {
			if ( windowChange == WindowChangeEventType.WindowShown ) {
				action = DialogAction.Cancel;
			}
		}

		private void OkButtonClicked( object source, RoutedEventArgs args ) {
			action = DialogAction.Ok;
			base.OnCloseClicked( null, null );
		}

		private void CancelButtonClicked( object source, RoutedEventArgs args ) {
			action = DialogAction.Cancel;
			base.OnCloseClicked( null, null );
		}

		public DialogAction Action {
			get {
				return action;
			}
		}
	}
}
